package com.tongtech.system.service.impl;

import com.tongtech.common.datascope.annotation.DataScope;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.InformationCategory;
import com.tongtech.system.domain.InformationCategorySubscribe;
import com.tongtech.system.mapper.InformationCategorySubscribeMapper;
import com.tongtech.system.service.IInformationCategoryService;
import com.tongtech.system.service.IInformationCategorySubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 资讯分类订阅管理 服务实现
 *
 * @author xingyc
 */
@Service
public class InformationCategorySubscribeServiceImpl implements IInformationCategorySubscribeService
{
    @Autowired
    private InformationCategorySubscribeMapper infoCategorySubscribeMapper;

    @Autowired
    private IInformationCategoryService infoCategoryService;

    /**
     * 查询资讯分类订阅管理数据
     *
     * @param infoCategorySubscribe 资讯分类订阅信息
     * @return 资讯分类订阅信息集合
     */
    @Override
    @DataScope(deptAlias = "infoCategorySubscribeSubscribe")
    public List<InformationCategorySubscribe> selectInformationCategorySubscribeList(InformationCategorySubscribe infoCategorySubscribe)
    {
        return infoCategorySubscribeMapper.selectInformationCategorySubscribeList(infoCategorySubscribe);
    }

    /**
     * 查询资讯分类订阅管理数据(过滤掉相同userid)
     *
     * @param infoCategorySubscribe 资讯分类订阅信息
     * @return 资讯分类订阅信息集合
     */
    @Override
    @DataScope(deptAlias = "infoCategorySubscribeSubscribe")
    public List<InformationCategorySubscribe> selectInformationCategorySubscribeListDistinctUserId(InformationCategorySubscribe infoCategorySubscribe)
    {
        return infoCategorySubscribeMapper.selectInformationCategorySubscribeListDistinctUserId(infoCategorySubscribe);
    }


    /**
     * 根据资讯分类订阅ID查询信息
     *
     * @param infoCategorySubscribeId 资讯分类订阅ID
     * @return 资讯分类订阅信息
     */
    @Override
    public InformationCategorySubscribe selectInformationCategorySubscribeById(Long infoCategorySubscribeId)
    {
        return infoCategorySubscribeMapper.selectInformationCategorySubscribeById(infoCategorySubscribeId);
    }

    /**
     * 根据资讯分类订阅ID查询信息
     *
     * @param userId 用户ID
     * @param categoryId 资讯分类D
     * @return 资讯分类订阅信息
     */
    @Override
    public InformationCategorySubscribe selectInformationCategorySubscribeByUserIdCategoryId(Long userId, Long categoryId) {
        return infoCategorySubscribeMapper.selectInformationCategorySubscribeByUserIdCategoryId(userId, categoryId);
    }

    /**
     * 新增保存资讯分类订阅信息
     *
     * @param infoCategorySubscribe 资讯分类订阅信息
     * @return 结果
     */
    @Override
    public int insertInformationCategorySubscribe(InformationCategorySubscribe infoCategorySubscribe)
    {
        Long categoryId = infoCategorySubscribe.getCategoryId();
        InformationCategory informationCategory = infoCategoryService.selectInformationCategoryById(categoryId);
        if(Objects.nonNull(informationCategory)){
            String categoryCode = informationCategory.getCategoryCode();
            String ancestors = informationCategory.getAncestors();
            Long userId = SecurityUtils.getUserId();
            infoCategorySubscribe.setCategoryCode(categoryCode);
            infoCategorySubscribe.setAncestors(ancestors);
            infoCategorySubscribe.setUserId(userId);
        }

        return infoCategorySubscribeMapper.insertInformationCategorySubscribe(infoCategorySubscribe);
    }

    /**
     * 修改保存资讯分类订阅信息
     *
     * @param infoCategorySubscribe 资讯分类订阅信息
     * @return 结果
     */
    @Override
    public int updateInformationCategorySubscribe(InformationCategorySubscribe infoCategorySubscribe)
    {
        int result = infoCategorySubscribeMapper.updateInformationCategorySubscribe(infoCategorySubscribe);
        return result;
    }

    /**
     * 删除资讯分类订阅管理信息
     *
     * @param deptId 资讯分类订阅ID
     * @return 结果
     */
    @Override
    public int deleteInformationCategorySubscribeById(Long deptId)
    {
        return infoCategorySubscribeMapper.deleteInformationCategorySubscribeById(deptId);
    }

    @Override
    public int deleteInformationCategorySubscribeByUserIdCategoryId(Long userId, Long categoryId) {
        return infoCategorySubscribeMapper.deleteInformationCategorySubscribeByUserIdCategoryId(userId, categoryId);
    }
}
